define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Themember, $timeout, $ionicSlideBoxDelegate,GoPage,GoDialog,$window,$ionicSideMenuDelegate) {

        $scope.input = new Object();
        $scope.input.keywords = '';
        $scope.input.comp_id = "";
        $scope.input.comp_select = {id:0,comp_name:"请选择快递公司"};
        $scope.cat_type = 11; //11为商品推荐
        $scope.recommend_id = 0; //推荐分类id，默认0，全部
        $scope.input.recommend = {id:0,name:"全部"};
		$scope.$on('$ionicView.beforeEnter', function() {
			//动态设置高度
			var lan = 0; //这是状态栏，只有ios的APP才有
			var all = $window.innerHeight;
			if(window.cordova && window.cordova.plugins) {
				if(ionic.Platform.isIOS()) {
					lan = 20;
				}
			}
			$scope.myObj = {
				"height": all - 44 - 44 - 35 - lan + "px"
			};
			
    		//获取用户信息：跳转链接用到
			Themember.getUserInfo(function(data){
				$scope.userInfo = data;
			})
		}, false);

        //加载数据
        $scope.init_data = function() {
            //获取推荐商品分类
            Themember.get_goodstype(function(data) {
                $scope.type = data.type;
            });
            Themember.get_list($scope.cat_type, $scope.input.keywords, 1, $scope.recommend_id, function(data) {
                $scope.listData = data;
                $scope.count = data.page.count;
            });
            //获取快递公司
            Themember.get_logisticscomp(function(data) {
                $scope.logisticscomp=data.logisticscomp;
            });
        }
        $scope.init_data();

        $scope.selectTab = 0;
        $scope.activeTab = function(index) {
            $scope.selectTab = index;
            $ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
        };
        //当滑动时，自动触发该事件
        $scope.slideChanged = function(index) {
            $scope.selectTab = index;
        };

		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

        //搜索
        $scope.doSearch = function(id) {
        	$scope.recommend_id = 0;//默认全部
        	if($scope.input.recommend){
        		$scope.recommend_id = $scope.input.recommend.id;
        	}
            //$scope.init_data();//最好不要这样写，因为该方法里包含了很多与搜索无关的其他方法
            Themember.get_list($scope.cat_type, $scope.input.keywords, 1, $scope.recommend_id, function(data) {
                $scope.listData = data;
                $scope.count = data.page.count;
                $scope.input.nPage = 1;
            });
        }
        //侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//关闭侧边菜单
		$scope.closeSideMenu = function() {
			if($ionicSideMenuDelegate.isOpen()){
				$ionicSideMenuDelegate.toggleRight();
			}
		};

		/*********************上下拉刷新*********************/
        $scope.isLoad = false; //可以加载
        $scope.input.nPage = 1;
        $scope.doRefresh = function() {
            //防止重复加载
//          if($scope.selectTab == 0) {
            	$scope.isLoad = true;
                Themember.get_list($scope.cat_type, $scope.input.keywords, 1, $scope.recommend_id, function(data) {
                    if(data != null) {
                        $scope.listData = data;
                    } else {
                        $scope.listData = new Object();
                    }
                    $scope.input.nPage = 1;
                },function() {
                    $scope.$broadcast('scroll.refreshComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
//          }
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
//          if($scope.selectTab == 0) {
	 			$scope.isLoad = true;
                Themember.get_list($scope.cat_type, $scope.input.keywords, $scope.input.nPage + 1, $scope.recommend_id, function(data) {
                    if(data != null) {
                        if($scope.listData != undefined) {
                            $scope.listData.data = $scope.listData.data.concat(data.data);
                            $scope.input.nPage += 1;
                        } else {
                            $scope.listData = data;
                        }
                    }
                },function() {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
//          }
        };
		/*********************上下拉刷新*********************/

        //跳转
        $scope.go_productdetail = function(goods_id){
            GoPage.go_states("productdetail_recommend",{"goods_id":goods_id});
        }

        //跳转到第三方外部链接
        $scope.onshow_outLink = function(){
        	$scope.outUrl = HTTP_URL_TRAIN;
			if(!$scope.userInfo){
				GoDialog.confirm("请求失败","请先登录",function(){
					GoPage.go_states("t-login");
				})
			}else if($scope.userInfo.verify_email!="Y"){//大写
				GoDialog.confirm("请求失败","请先绑定邮箱",function(){
					GoPage.go_states("linkemail");
				})
			}else{
				GoPage.go_states('train');
				//$scope.outUrl = $scope.outUrl+"?session="+$scope.userInfo.session;//要把session字符串传过去
				//GoPage.go_link($scope.outUrl);
			}
		}

        //查询快递
         $scope.search_logistics=function(){
             if(!$scope.input.logistics_no){
                GoDialog.toast("请输入您的快递单号！");
             }else{
             	Themember.get_logistics($scope.input.comp_id,$scope.input.logistics_no,function(data){
                    $scope.logistics=data;
                    $scope.input.comp_id = data.comCode;
           		});
            }
        }

		//跳转到“滞销产品”和“经营转让”
		$scope.onshow_url = function(status){
			GoPage.go_states(status);
		}

		$scope.doSelectCat = function(){
			//id是值 name是显示
			GoDialog.showSelect("类目",$scope.type,$scope.input.recommend,function(data){
				$scope.input.recommend = data;
			});
		}
		$scope.doSelectComp = function(){
			//id是值 name是显示
			GoDialog.showSelect("快递",$scope.logisticscomp,$scope.input.comp_select,function(data){
                $scope.input.comp_id = data.corp_code;
				$scope.input.comp_select = data;
			});
		}
    }

    controller.$inject = ['$scope', 'Themember', '$timeout', '$ionicSlideBoxDelegate','GoPage','GoDialog','$window','$ionicSideMenuDelegate'];

    return app.registerController("thememberctrl",controller);
});